package com.sec.android.app.myfiles.dialog;

import android.app.DialogFragment;
import android.app.Fragment;
import android.text.TextUtils;

/**
 * Created by jaebae on 2016-06-01.
 */
public class DialogFragmentUtils {
    private DialogFragmentUtils() {
    }


    /**
     * <pre>
     * if target fragment is not exist in fragment manager, IllegalStateException will occur.
     * so, check the target fragment and remove it if target fragment is not exist in fragment manager.
     * </pre>
     * @param dialogFragment
     */
    public static void setTargetFragment(DialogFragment dialogFragment) {
        Fragment targetFragment = dialogFragment.getTargetFragment();

        if (targetFragment != null) {
            String tag = targetFragment.getTag();
            if (!TextUtils.isEmpty(tag)) {
                Fragment findFragment = dialogFragment.getFragmentManager().findFragmentByTag(tag);
                if (findFragment == null) {
                    dialogFragment.setTargetFragment(null, -1);
                }
            } else {
                dialogFragment.setTargetFragment(null, -1);
            }
        }
    }
}
